package org.jeecg.modules.lab.util;

import io.swagger.models.auth.In;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.lab.sample.code.service.ILabSampleCodeService;
import org.jeecg.modules.lab.sample.entity.LabSampleCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import redis.clients.jedis.Jedis;

import javax.annotation.PostConstruct;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * 使用redis生成流水号
 */
@Component
@Repository
public class FlowNumberUtil {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private ILabSampleCodeService labSampleCodeService;


    public List<String> generateSerialNum(String rule, Integer num) {
        List<String> listRule = new ArrayList<>();
        String codeRule = redisTemplate.opsForValue().get(rule);
        if (codeRule != null || !codeRule.equals("")) {
            listRule = createSerialNum(listRule, codeRule, num, rule);
        }
        return listRule;
    }


    public List<String> createSerialNum(List<String> listRule, String codeRule, Integer num, String rule) {
        String serialNum = "";
        DecimalFormat dft = new DecimalFormat("000");

        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String date = formatter.format(now);

        String year = date.substring(0, 4);
        String month = date.substring(4, 6);
        String day = date.substring(6, 8);

        String key = year + rule + "SamSerialNum";

        for (int i = 0; i < num; i++) {
            Long count = redisTemplate.opsForValue().increment(key, 1);
            serialNum = dft.format(count);
            String serialNumCode = codeRule.replace("YYYY", year).replace("MM", month).replace("dd", day).replace("001", serialNum);
            listRule.add(serialNumCode);
        }
        return listRule;
    }


    /**
     * 初始化编码规则数据
     */
    public void initialCodeRule() {
        //初始化样品编码规则
        List<LabSampleCode> labSampleCodeList = labSampleCodeService.list();
        labSampleCodeList.forEach(labSampleCode -> {
            redisTemplate.opsForValue().set(labSampleCode.getCodePrefix(), labSampleCode.getCodeRule());
        });
    }

    /**
     * 生成随机id号
     */
    public static String getRandomId() {
        //取当前时间的长整形值包含毫秒
        long millis = System.currentTimeMillis();
        //加上两位随机数
        Random random = new Random();
        int end2 = random.nextInt(99);
        //如果不足两位前面补0
        String str = millis + String.format("%02d", end2);
        long id = new Long(str);
        return Long.toString(id);
    }


    public String utensilialNum() {

        DecimalFormat dft = new DecimalFormat("0000");
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String date = formatter.format(now);

        String year = date.substring(0, 4);
        String month = date.substring(4, 6);
        //String day = date.substring(6, 8);

        String key = "QJ" + year;
        Long count = redisTemplate.opsForValue().increment(key, 1);
        String serialNum = dft.format(count);
        String serialNumCode = key + month + serialNum;

        return serialNumCode;
    }


}
